package com.keur.money.utils.dialog

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.keur.money.R
import com.keur.money.app.MyApp
import com.keur.money.base.utils.DensityUtils
import com.keur.money.databinding.DialogMullaCouponExchangeBinding
import com.keur.money.http.HttpHelper
import com.keur.money.utils.event.KeurEventType
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/9 13:45
 * @Annotation：优惠券兑换dialog
 */
class CouponExchangeDialog: ProDialog {
    private lateinit var binding: DialogMullaCouponExchangeBinding
    private var mCallback: IDialogCallback? = null

    var couponData: JSONObject? = null

    override fun initWindow() {
        val params = window?.attributes
        params?.width = MyApp.app.getPhoneWidth() - DensityUtils.dp2px(30f)
        params?.height = WindowManager.LayoutParams.WRAP_CONTENT
        window?.attributes = params
        window?.setWindowAnimations(R.style.picker_view_slide_anim) //修改动画样式
        window?.setGravity(Gravity.BOTTOM)
    }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogMullaCouponExchangeBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
    }

    override fun initListener() {
        setOnClickListener(R.id.imgClose) //关闭
        setOnClickListener(R.id.btnExchange)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        when(v?.id){
            R.id.btnExchange -> {
                mCallback?.onMullaExchangeAction(couponData)
            }
        }
    }

    /**
     * 设置需要兑换的优惠券信息
     */
    @SuppressLint("SetTextI18n")
    fun setMullaCouponInfo(couponInfo: JSONObject){
        couponData = couponInfo
        val amount = couponInfo.optString("amount")
        val unit = couponInfo.optString("currency")
        binding.textAmount.text = amount
        binding.textUnit.text = unit
        binding.textConsumeTips.text = String.format(getString(R.string.coupon_exchange_dialog_title),"$amount $unit",couponInfo.optString("fullAmount"))

        val showTipsText = String.format(getString(R.string.coupon_exchange_dialog_amount),"1")
        val spString = SpannableString(showTipsText)
        spString.setSpan(ForegroundColorSpan(Color.parseColor("#FF000000")), showTipsText.length - 1, showTipsText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding.textCount.text = spString
        binding.textSureConsume.text = String.format(getString(R.string.coupon_exchange_dialog_consume),couponInfo.optString("score"))

    }

    interface IDialogCallback {
        //按钮点击
        fun onMullaExchangeAction(couponInfo: JSONObject?)
    }

    fun getString(strId: Int): String{
        return MyApp.app.getString(strId)
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_COUPON_EXCHANGE_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addKeurEventAction(KeurEventType.DIALOG.KEUR_DIALOG_COUPON_EXCHANGE_DISMISS)
    }
}